﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace WorkItemManager {
	public partial class UIPrincipal : Form {
		TeamFoundationServer _TFSServer = null;
		WorkItemStore _WorkItemStore = null;

		public UIPrincipal() {
			InitializeComponent();

			grpWorkItems.Enabled = false;
			btnConnect.Enabled = false;
		}

		private void btnDeleteSelected_Click(object sender, EventArgs e) {
			List<WorkItemInfo> selectedWorkItems = new List<WorkItemInfo>();
			foreach (DataGridViewRow row in grdWorkItems.SelectedRows) {
				selectedWorkItems.Add((WorkItemInfo)row.DataBoundItem);
			}

			UIDelete ui = new UIDelete(_TFSServer, selectedWorkItems);
			if (ui.ShowDialog() == DialogResult.OK) {
				UpdateWorkItems();
			}
		}

		private void btnConnect_Click(object sender, EventArgs e) {
			try {
				Cursor.Current = Cursors.WaitCursor;

				if (ConnectTFSServer(txtTFSServer.Text)) {
					_WorkItemStore = (WorkItemStore)_TFSServer.GetService(typeof(WorkItemStore));

					QueryTeamProjects();

					grpWorkItems.Enabled = true;
				}
			} finally {
				Cursor.Current = Cursors.Default;
			}
		}

		private void cboTeamProject_SelectedIndexChanged(object sender, EventArgs e) {
			UpdateWorkItems();
		}

		private void UpdateWorkItems() {
			try {
				Cursor.Current = Cursors.WaitCursor;

				ProjectInfo selectedProject = (ProjectInfo)cboTeamProject.SelectedItem;
				string query = string.Format("SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '{0}' ORDER BY [System.Id]", selectedProject.Name);
				WorkItemCollection collection = _WorkItemStore.Query(query);

				List<WorkItemInfo> workItemList = new List<WorkItemInfo>();
				foreach (WorkItem item in collection) {
					workItemList.Add(new WorkItemInfo(item));
				}

				grdWorkItems.DataSource = workItemList;
			} finally {
				Cursor.Current = Cursors.Default;
			}
		}

		private void txtTFSServer_TextChanged(object sender, EventArgs e) {
			btnConnect.Enabled = !string.IsNullOrEmpty(txtTFSServer.Text);
		}

		private bool ConnectTFSServer(string serverAddress) {
			_TFSServer = new TeamFoundationServer(serverAddress);

			try {
				_TFSServer.Authenticate();
			} catch (Exception ex) {
				MessageBox.Show("Cannot connect to Team Foundation Server. Error:\n" + ex.Message);
				return false;
			}
			return true;
		}

		private void QueryTeamProjects() {
			ICommonStructureService css = (ICommonStructureService)_TFSServer.GetService(typeof(ICommonStructureService));
			ProjectInfo[] projects = css.ListAllProjects();
			projects = projects.OrderBy<ProjectInfo, string>(p => p.Name).ToArray();

			cboTeamProject.DataSource = projects;
		}
	}
}
